<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/1 0001
 * Time: 16:38
 */

namespace controllers;


class BaseControllers
{
    protected $twig;
    protected $data = array();
    public function __construct()
    {
        $loader = new \Twig\Loader\FilesystemLoader(TEMPDIR.'/app/views');
        $this->twig = new \Twig\Environment($loader, [
            //'cache' => '/path/to/compilation_cache',
        ]);
    }
    protected function assign($var,$value=null){
        if(is_array($var)){
            $this->data = array_merge($var,$this->data);
        }else{
            $this->data[$var] = $value;
        }
    }
    protected function display($template){
        $url = getCurUrl();
        $this->assign('uri',$url);
        echo $this->twig->render($template.'.html', $this->data);
    }

    protected function topsuccess($url, $mess) {
        echo "<script>";
        echo "alert('{$mess}');";

        if(!empty($url)) {
            echo "top.location.href='{$url}';";
        }

        echo "</script>";
    }



    /**
     * @param $url
     * @param $mess
     * 成功跳转
     */
    protected function success($url,$mess)
    {
        echo "<script>";
        echo "alert('{$mess}');";
        if(!empty($url)){
            echo "location.href='{$url}';";
        }
        echo "</script>";
    }

    /**
     * @param $url
     * @param $mess
     * 失败跳转
     */
    protected function error($url,$mess)
    {
        echo "<script>";
        echo "alert('ERROR:{$mess}');";
        if(!empty($url)){
            echo "location.href='{$url}';";
        }
        echo "</script>";
    }
}